package cqrtplm.service.impl;



import com.alibaba.cloud.commons.lang.StringUtils;
import com.hustcad.plm.pdm.user.service.TyplmPrincipalService;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.PrincipalEntity;
import cqrtplm.dto.query.RtSegmNumQueryDTO;
import cqrtplm.entity.RtSegmNumDO;
import cqrtplm.entity.RtZzdhDO;
import cqrtplm.mapper.RtSegmNumMapper;
import cqrtplm.mapper.RtZzdhMapper;
import cqrtplm.service.RtZzdhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class RtZzdhServiceImpl implements RtZzdhService {
    @Autowired
    private RtZzdhMapper rtZzdhMapper;
    @Autowired
    private RtSegmNumMapper rtSegmNumMapper;
    @Override
    public int save(RtZzdhDO record) {
        return rtZzdhMapper.insert(record);
    }
    @Override
    public int update(RtZzdhDO record) {
        return rtZzdhMapper.update(record);
    }
    @Override
    public int delete(String id) {
        return rtZzdhMapper.delete(id);
    }
    @Override
    public RtZzdhDO getById(String id) {
        return rtZzdhMapper.selectById(id);
    }
    @Override
    public List<RtZzdhDO> listAll() {
        return rtZzdhMapper.selectAll();
    }
    @Override
    public List<RtZzdhDO> listByGroup(String group) {
        return rtZzdhMapper.selectByGroup(group);
    }
    @Autowired
    private TyplmPrincipalService typlmPrincipalService;

    /**
     * 检查是否有物料号权限
     * @param material 物料号
     * @param userOid 用户oid
     * @return 是否有权限
     */
    @Override
    public boolean hasPermission(String material, PrincipalEntity userOid) {
        // 1. 根据物料号查找对应的段号列表
        List<String> segmentNumbers = findSegmentNumbersByMaterial(material);
        if (segmentNumbers.isEmpty()) {
            return false; // 物料号没有关联任何段号，直接返回无权限
        }
        // 2. 获取这些段号对应的去重群组集合
        Set<String> groups = findGroupsBySegmentNumbers(segmentNumbers);
        if (groups.isEmpty()) {
            return false; // 段号没有关联任何群组，直接返回无权限
        }
        // 3. 检查用户是否属于这些群组
        for (String group:groups) {
            IdentifierEntity groupID = new IdentifierEntity(new BigInteger(group),"ty.inteplm.org.CTyGroup");
            PrincipalEntity groupEntity = typlmPrincipalService.getPrincipalByIdentifier(groupID);
            if(typlmPrincipalService.isMember(userOid,groupEntity)){
                return true;
            }
        }
        return false;
    }

    /**
     * 根据物料号查找对应的段号列表
     * @param material 物料号
     * @return 关联的段号列表
     */
    private List<String> findSegmentNumbersByMaterial(String material) {
        // 查询所有段号记录
        List<RtSegmNumDO> allSegments = rtSegmNumMapper.selectAll(new RtSegmNumQueryDTO());
        // 筛选包含该物料号的段号
        return allSegments.stream()
                .filter(segment -> {
                    String parts = segment.getParts();
                    if (StringUtils.isBlank(parts)) {
                        return false;
                    }
                    // 分割PARTS字段中的物料号列表
                    List<String> materials = Arrays.asList(parts.split(","));
                    return materials.contains(material);
                })
                .map(RtSegmNumDO::getId)
                .collect(Collectors.toList());
    }



    /**
     * 根据段号列表查找对应的群组集合(去重)
     * @param segmentNumbers 段号列表
     * @return 关联的群组集合
     */
    private Set<String> findGroupsBySegmentNumbers(List<String> segmentNumbers) {
        if (segmentNumbers == null || segmentNumbers.isEmpty()) {
            return Collections.emptySet();
        }
        // 查询所有符合条件的ZZDH关系
        List<RtZzdhDO> relations = rtZzdhMapper.selectBySegmentNumbers(segmentNumbers);
        // 提取并去重群组
        return relations.stream()
                .map(RtZzdhDO::getGroup1)
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.toSet());
    }

}
